package com.njitzx.serivce.impl;

import com.njitzx.entity.Student;
import com.njitzx.entity.vo.*;
import com.njitzx.mapper.StudentMapper;
import com.njitzx.mapper.TeacherMapper;
import com.njitzx.serivce.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.LinkedHashMap;
@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {

    private final StudentMapper studentMapper;

    private final TeacherMapper teacherMapper;

    @Override
    public CollegeVO getCollege() {
        List<Student> students = studentMapper.getAll();

        // 分组并计算每个学院的学生数量
        Map<String, Long> collect = students.stream()
                .collect(Collectors.groupingBy(Student::getCollege, Collectors.counting()));

        // 根据值进行排序（降序），然后将结果收集到一个新的LinkedHashMap中
        Map<String, Long> sortedCollect = collect.entrySet().stream()
                .sorted(Entry.<String, Long>comparingByValue().reversed())
                .collect(Collectors.toMap(
                        Entry::getKey,
                        Entry::getValue,
                        (e1, e2) -> e1,
                        LinkedHashMap::new
                ));

        // 将排序后的键和值拼接成字符串
        String college = String.join(",", sortedCollect.keySet());
        String countList = sortedCollect.values().stream()
                .map(String::valueOf)
                .collect(Collectors.joining(","));

        return CollegeVO.builder().nameList(college).numberList(countList).build();
    }

    @Override
    public List<GenderVO> getSex() {
        List<Student> students = studentMapper.getAll();
        Map<String, Long> collect = students.stream()
                .collect(Collectors.groupingBy(Student::getGender, Collectors.counting()));
        List<GenderVO> genderVOList = collect.entrySet().stream()
                .map(entry -> {
                    GenderVO vo = new GenderVO();
                    vo.setName(entry.getKey());
                    vo.setValue(entry.getValue());
                    return vo;
                })
                .collect(Collectors.toList());
        return  genderVOList;
    }

    @Override
    public List<ProvinceVO> getProvince() {
        List<Student> students = studentMapper.getAll();
        Map<String, Long> collect = students.stream()
                .collect(Collectors.groupingBy(Student::getProvinces, Collectors.counting()));
        List<ProvinceVO> genderVOList = collect.entrySet().stream()
                .map(entry -> {
                    ProvinceVO vo = new ProvinceVO();
                    vo.setName(entry.getKey());
                    vo.setValue(entry.getValue());
                    return vo;
                })
                .collect(Collectors.toList());

        return  genderVOList;
    }
    @Override
    public NumberVO getnumbwer() {
        Long l = studentMapper.selectCount();
        Long r = teacherMapper.selectCount();
        return  NumberVO.builder().numberStudent(l).numberTeacher(r).build();
    }

    @Override
    public HobbyVO getHobby() {
        List<Student> students = studentMapper.getAll();

        // 分组并计算每个学院的学生数量
        Map<String, Long> collect = students.stream()
                .collect(Collectors.groupingBy(Student::getHobby, Collectors.counting()));

        // 将排序后的键和值拼接成字符串
        String name = String.join(",", collect.keySet());
        String count = collect.values().stream()
                .map(String::valueOf)
                .collect(Collectors.joining(","));

        return HobbyVO.builder().name(name).count(count).build();
    }
}
